package cn.dida.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.dida.exception.BizzException;
import cn.dida.model.HttpResponseBodyWrapper;
import cn.dida.model.Pageable;
import cn.dida.model.Region;
import cn.dida.model.UserSession;
import cn.dida.service.RegionService;

/**
 * <p>地区列表</p>
 * @author zhangzhiling
 */
@Controller
public class RegionController extends BaseController {

    @Autowired
    private RegionService regionService;


    /**
     * 获取地区列表
     * @param request
     * @param parentId 父节点id
     * @param page 分页游标
     * @return ModelAndView 地区列表页面
     * @throws Exception
     */
    @GetMapping("/region")
    public ModelAndView getRegionlist(HttpServletRequest request, Integer parentId, Integer page) throws Exception {

        Map<String, Object> model = new HashMap<String, Object>();
        page = (page == null || page < 1) ? 1 : page;
        Pageable<Region> regionList = regionService.getRegionList(parentId, page);

        model.put("regionList", regionList);
        model.put("parentId", parentId);
        return new ModelAndView("region_list").addAllObjects(model);
    }


    /**
     * 添加
     * @param request
     * @param region 地区实体信息
     * @return HttpResponseBodyWrapper 封装操作标记（ true新增成功，false新增失败）
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/setRegion")
    public HttpResponseBodyWrapper setRegion(HttpServletRequest request, @RequestBody Region region) throws Exception {

        UserSession userSession = getUserSession(request);

        if (region.getName() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请输入地区名称");
        }
        if (region.getParentId() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请输入父级id");
        }
        if (region.getRegionType() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请输入地区级别");
        }
        if(regionService.isExit(region.getParentId(), region.getName()) != null){
            throw new BizzException(BizzException.error_code_invalid_params, "地区已存在");
        }
        Integer result = regionService.insert(region);
        return new HttpResponseBodyWrapper(result == 1);
    }

    /**
     * 编辑修改
     * @param request
     * @param region 地区实体信息
     * @return HttpResponseBodyWrapper 封装操作标记（ true更新成功，false更新失败）
     * @throws Exception
     */
    @ResponseBody
    @PutMapping("/updateRegion")
    public HttpResponseBodyWrapper updateRegion(HttpServletRequest request, @RequestBody Region region) throws Exception {

        UserSession userSession = getUserSession(request);

        if (region.getId() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请选择要修改的地区");
        }
        if (region.getName() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请输入地区名称");
        }
        if (region.getParentId() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请输入父级id");
        }
        if (region.getRegionType() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请输入地区级别");
        }

        if(regionService.isExit(region.getParentId(), region.getName()) != null){
            throw new BizzException(BizzException.error_code_invalid_params, "地区已存在");
        }
        Integer result = regionService.update(region);
        return new HttpResponseBodyWrapper(result == 1);
    }

    /**
     * 删除（is_show字段置为0）
     * @param id 地区id
     * @return HttpResponseBodyWrapper 封装操作标记（ true删除成功，false删除失败）
     * @throws Exception
     */
    @ResponseBody
    @DeleteMapping("/deleteRegion/{id}")
    public HttpResponseBodyWrapper deleteRegion( @PathVariable Integer id) throws Exception {
        Integer result = regionService.delete(id);
        return new HttpResponseBodyWrapper(result == 1);
    }

    /**
     * 获取详情
     * @param id 地区id
     * @return HttpResponseBodyWrapper 包装单个地区实体信息
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/gteRegion/{id}")
    public HttpResponseBodyWrapper gteRegion( @PathVariable int id) throws Exception {
        Region region = regionService.getPrizeById(id);
        return new HttpResponseBodyWrapper(region);
    }




}
